Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix problem #960

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/angry-worms-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'microbundle': patch
---

Fix css compilation problems when multiple entries
5 changes: 5 additions & 0 deletions .changeset/bright-ravens-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'microbundle': patch
---

Fix problems related to use rollup-plugin-terser
141 changes: 75 additions & 66 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,7 @@ export default async function microbundle(inputOptions) {
for (let i = 0; i < options.entries.length; i++) {
for (let j = 0; j < formats.length; j++) {
steps.push(
createConfig(
options,
options.entries[i],
formats[j],
i === 0 && j === 0,
),
createConfig(options, options.entries[i], formats[j], j === 0),
);
}
}
Expand Down Expand Up @@ -333,6 +328,7 @@ function getMain({ options, entry, format }) {
const shebang = {};

function createConfig(options, entry, format, writeMeta) {
const entryFileName = basename(entry, extname(entry));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incorrect. Please use the existing naming setup that we had, it ensures names are properly stripped:

options.output.replace(EXTENSION, '.css'),

index.esm.js as an input needs to become index; yours would have it become index.esm, so index.esm.css would be output. We need to strip indicators towards ES module format, i.e., cjs, es[m], etc.

let { pkg } = options;

/** @type {(string|RegExp)[]} */
Expand Down Expand Up @@ -390,15 +386,15 @@ function createConfig(options, entry, format, writeMeta) {

// let rollupName = safeVariableName(basename(entry).replace(/\.js$/, ''));

let nameCache = {};
const bareNameCache = nameCache;
// Support "minify" field and legacy "mangle" field via package.json:
const rawMinifyValue = options.pkg.minify || options.pkg.mangle || {};
let minifyOptions = typeof rawMinifyValue === 'string' ? {} : rawMinifyValue;
const getNameCachePath =
typeof rawMinifyValue === 'string'
? () => resolve(options.cwd, rawMinifyValue)
: () => resolve(options.cwd, 'mangle.json');
let minifyOptions = typeof rawMinifyValue === 'string' ? {} : rawMinifyValue;
let endsWithNewLine = false;
let terserOptions = {};

const useTypescript = extname(entry) === '.ts' || extname(entry) === '.tsx';
const emitDeclaration =
Expand All @@ -415,29 +411,6 @@ function createConfig(options, entry, format, writeMeta) {
const externalTest =
external.length === 0 ? id => false : id => externalPredicate.test(id);

let endsWithNewLine = false;

function loadNameCache() {
try {
const data = fs.readFileSync(getNameCachePath(), 'utf8');
endsWithNewLine = data.endsWith(EOL);
nameCache = JSON.parse(data);
// mangle.json can contain a "minify" field, same format as the pkg.mangle:
if (nameCache.minify) {
minifyOptions = Object.assign(
{},
minifyOptions || {},
nameCache.minify,
);
}
} catch (e) {}
}
loadNameCache();

normalizeMinifyOptions(minifyOptions);

if (nameCache === bareNameCache) nameCache = null;

/** @type {false | import('rollup').RollupCache} */
let cache;
if (modern) cache = false;
Expand Down Expand Up @@ -502,7 +475,7 @@ function createConfig(options, entry, format, writeMeta) {
extract:
!!writeMeta &&
options.css !== 'inline' &&
options.output.replace(EXTENSION, '.css'),
resolve(outputDir, `${entryFileName}.css`),
Comment on lines -505 to +478
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather we not use outputDir as it isn't guaranteed to match up with expectations in the future (see #896)

CSS should continue to be output according to the result of getOutput for resilience IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about changing it to this

minimize: options.compress,
sourceMap: options.sourcemap && options.css !== 'inline',
}),
Expand Down Expand Up @@ -601,43 +574,79 @@ function createConfig(options, entry, format, writeMeta) {
},
}),
options.compress !== false && [
terser({
compress: Object.assign(
{
keep_infinity: true,
pure_getters: true,
// Ideally we'd just get Terser to respect existing Arrow functions...
// unsafe_arrows: true,
passes: 10,
},
typeof minifyOptions.compress === 'boolean'
? minifyOptions.compress
: minifyOptions.compress || {},
),
format: {
// By default, Terser wraps function arguments in extra parens to trigger eager parsing.
// Whether this is a good idea is way too specific to guess, so we optimize for size by default:
wrap_func_args: false,
comments: /^\s*([@#]__[A-Z]+__\s*$|@cc_on)/,
preserve_annotations: true,
},
module: modern,
ecma: modern ? 2017 : 5,
toplevel: modern || format === 'cjs' || format === 'es',
mangle:
typeof minifyOptions.mangle === 'boolean'
? minifyOptions.mangle
: Object.assign({}, minifyOptions.mangle || {}),
nameCache,
}),
nameCache && {
terser(terserOptions),
{
// before hook
options: loadNameCache,
options() {
let nameCache = {};
const bareNameCache = nameCache;

function loadNameCache() {
try {
const data = fs.readFileSync(getNameCachePath(), 'utf8');
endsWithNewLine = data.endsWith(EOL);
nameCache = JSON.parse(data);
// mangle.json can contain a "minify" field, same format as the pkg.mangle:
if (nameCache.minify) {
minifyOptions = Object.assign(
{},
minifyOptions || {},
nameCache.minify,
);
}
} catch (e) {}
}
loadNameCache();

normalizeMinifyOptions(minifyOptions);

if (nameCache === bareNameCache) nameCache = null;

Object.entries({
compress: Object.assign(
{
keep_infinity: true,
pure_getters: true,
// Ideally we'd just get Terser to respect existing Arrow functions...
// unsafe_arrows: true,
passes: 10,
},
typeof minifyOptions.compress === 'boolean'
? minifyOptions.compress
: minifyOptions.compress || {},
),
format: {
// By default, Terser wraps function arguments in extra parens to trigger eager parsing.
// Whether this is a good idea is way too specific to guess, so we optimize for size by default:
wrap_func_args: false,
comments: /^\s*([@#]__[A-Z]+__\s*$|@cc_on)/,
preserve_annotations: true,
},
module: modern,
ecma: modern ? 2017 : 5,
toplevel: modern || format === 'cjs' || format === 'es',
mangle:
typeof minifyOptions.mangle === 'boolean'
? minifyOptions.mangle
: Object.assign({}, minifyOptions.mangle || {}),
nameCache,
}).forEach(([key, value]) => {
terserOptions[key] = value;
});
},
// after hook
writeBundle() {
if (writeMeta && nameCache) {
if (writeMeta && terserOptions.nameCache) {
try {
if (
terserOptions.nameCache.minify.mangle.properties.regex
) {
terserOptions.nameCache.minify.mangle.properties.regex =
terserOptions.nameCache.minify.mangle.properties.regex.source;
}
} catch (error) {}
let filename = getNameCachePath();
let json = JSON.stringify(nameCache, null, 2);
let json = JSON.stringify(terserOptions.nameCache, null, 2);
if (endsWithNewLine) json += EOL;
fs.writeFile(filename, json, () => {});
}
Expand Down