'use strict'; var pluginutils = require('@rollup/pluginutils'); var core = require('@babel/core'); var babelPlugin = require('astroturf/plugin'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var babelPlugin__default = /*#__PURE__*/_interopDefaultLegacy(babelPlugin); function astroturf({include, exclude, ...rest} = {}) { const filter = pluginutils.createFilter(include || /\.(jsx?|tsx?)/i, exclude); const cssLookup = {}; const pathMap = {}; return { name: 'astroturf', load(id) { return cssLookup[id]; }, resolveId(importee) { if (importee in cssLookup) return cssLookup[importee]; if (importee in pathMap) return pathMap[importee]; }, transform(code, id) { if (!filter(id)) return const {code: transformedCode, generatedFiles, sourceMap} = transform(code, { filename: id, ...rest }); Object.assign(cssLookup, Object.fromEntries(generatedFiles.map(({fullPath, code}) => ([fullPath, code])))); Object.assign(pathMap, Object.fromEntries(generatedFiles.map(({importPath, fullPath}) => ([importPath, fullPath])))); return { code: transformedCode, map: sourceMap }; } } } function transform(code, {filename, plugins, ...rest} = {}) { plugins = plugins || []; const ast = core.parseSync(code, { filename, plugins: [...plugins], caller: { name: 'astroturf' } }); const {metadata, code: transformedCode, map} = core.transformFromAstSync(ast, code, { filename, babelrc: false, configFile: false, sourceMaps: true, sourceFileName: filename, plugins: [...plugins, [babelPlugin__default['default'], {...rest, writeFiles: false}]], inputSourceMap: undefined }); const generatedFiles = metadata.astroturf.styles .map(({absoluteFilePath, requirePath, value}) => ({importPath: requirePath, fullPath: absoluteFilePath, code: value})); return { code: transformedCode || '', generatedFiles, sourceMap: map } } module.exports = astroturf;