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: fix build for dynamic load postcss plugin #1292

Merged
merged 2 commits into from
Jan 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions packages/vite/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,12 @@ const nodeConfig = {
replacement: `eval('require')('sugarss')`
},
'import-fresh/index.js': {
src: 'require(filePath)',
pattern: /require\(filePath\)/g,
Copy link
Member

Choose a reason for hiding this comment

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

This change was unnecessary and caused #1306

replacement: `eval('require')(filePath)`
},
'import-from/index.js': {
pattern: /require\(resolveFrom/g,
replacement: `eval('require')(resolveFrom`
}
}),
// Optional peer deps of ws. Native deps that are mostly for performance.
Expand Down Expand Up @@ -159,7 +163,7 @@ const terserConfig = {
}

/**
* @type { (deps: Record<string, { src: string, replacement: string }>) => import('rollup').Plugin }
* @type { (deps: Record<string, { src?: string, replacement: string, pattern?: string }>) => import('rollup').Plugin }
*/
function shimDepsPlugin(deps) {
const transformed = {}
Expand All @@ -169,15 +173,37 @@ function shimDepsPlugin(deps) {
transform(code, id) {
for (const file in deps) {
if (slash(id).endsWith(file)) {
const { src, replacement } = deps[file]
const pos = code.indexOf(src)
if (pos < 0) {
this.error(`Could not find expected src "${src}" in file "${file}"`)
}
const { src, replacement, pattern } = deps[file]

const magicString = new MagicString(code)
magicString.overwrite(pos, pos + src.length, replacement)
transformed[file] = true
console.log(`shimmed: ${file}`)
if (src) {
const pos = code.indexOf(src)
if (pos < 0) {
this.error(
`Could not find expected src "${src}" in file "${file}"`
)
}
transformed[file] = true
magicString.overwrite(pos, pos + src.length, replacement)
console.log(`shimmed: ${file}`)
}

if (pattern) {
let match
while ((match = pattern.exec(code))) {
transformed[file] = true
const start = match.index
const end = start + match[0].length
magicString.overwrite(start, end, replacement)
}
if (!transformed[file]) {
this.error(
`Could not find expected pattern "${pattern}" in file "${file}"`
)
}
console.log(`shimmed: ${file}`)
}

return {
code: magicString.toString(),
map: magicString.generateMap({ hires: true })
Expand Down