Skip to content

Commit

Permalink
feat: improve regex match, close #4
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Feb 3, 2023
1 parent 1bab1c9 commit 2a35abf
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
1 change: 1 addition & 0 deletions .eslintignore
@@ -0,0 +1 @@
fixtures
2 changes: 1 addition & 1 deletion src/regex.ts
@@ -1,4 +1,4 @@
const multilineCommentsRE = /\/\*.*?\*\//gms
const multilineCommentsRE = /\/\*([^*\/])*?\*\//gms
const singlelineCommentsRE = /(?:^|\n|\r)\s*\/\/.*(?:\r|\n|$)/gm
const templateLiteralRE = /\$\{(\s*(?:(?!\$\{).|\n|\r)*?\s*)\}/g
const quotesRE = [
Expand Down
31 changes: 31 additions & 0 deletions test/fixtures/issue4.ts
@@ -0,0 +1,31 @@
import __variableDynamicImportRuntimeHelper from "vite/dynamic-import-helper";
const getTestData = async () => {
const filename = "message";
console.log(await __variableDynamicImportRuntimeHelper((import.meta.glob("./data/something/*.json")), `./data/something/${filename}.json`));
console.log(await __variableDynamicImportRuntimeHelper((import.meta.glob("./data/whatever/*.json")), `./data/whatever/${filename}.json`));
};

getTestData();
const _sfc_main = {};

import { mergeProps as _mergeProps } from "vue"
import { ssrRenderAttrs as _ssrRenderAttrs, ssrInterpolate as _ssrInterpolate } from "vue/server-renderer"

function _sfc_ssrRender(_ctx, _push, _parent, _attrs, $props, $setup, $data, $options) {
_push(`<div${
_ssrRenderAttrs(_mergeProps({ class: "whatever" }, _attrs))
}>${
_ssrInterpolate(new Date())
}</div>`)
}


import { useSSRContext as __vite_useSSRContext } from 'vue'
const _sfc_setup = _sfc_main.setup
_sfc_main.setup = (props, ctx) => {
const ssrContext = __vite_useSSRContext()
;(ssrContext.modules || (ssrContext.modules = new Set())).add("App.vue")
return _sfc_setup ? _sfc_setup(props, ctx) : undefined
}
import _export_sfc from 'plugin-vue:export-helper'
export default /*#__PURE__*/_export_sfc(_sfc_main, [['ssrRender',_sfc_ssrRender],['__file',"/Users/aryse/Projects/open-source/vite-ssr-issue-vue-sfc-dynamic-import/App.vue"]])
37 changes: 37 additions & 0 deletions test/index.test.ts
@@ -1,4 +1,5 @@
/* eslint-disable no-template-curly-in-string */
import fs from 'fs/promises'
import { parse } from 'acorn'
import { expect, test } from 'vitest'
import { stripLiteral } from '../src'
Expand Down Expand Up @@ -206,3 +207,39 @@ test('// in string', () => {
"
`)
})

test('#4', async () => {
const str = await fs.readFile('./test/fixtures/issue4.ts', 'utf-8')
expect(executeWithVerify(str, false)).toMatchInlineSnapshot(`
"import __variableDynamicImportRuntimeHelper from \\" \\";
const getTestData = async () => {
const filename = \\" \\";
console.log(await __variableDynamicImportRuntimeHelper((import.meta.glob(\\" \\")), \` \${filename} \`));
console.log(await __variableDynamicImportRuntimeHelper((import.meta.glob(\\" \\")), \` \${filename} \`));
};
getTestData();
const _sfc_main = {};
import { mergeProps as _mergeProps } from \\" \\"
import { ssrRenderAttrs as _ssrRenderAttrs, ssrInterpolate as _ssrInterpolate } from \\" \\"
function _sfc_ssrRender(_ctx, _push, _parent, _attrs, $props, $setup, $data, $options) {
_push(\` \${
_ssrRenderAttrs(_mergeProps({ class: \\" \\" } \${
_ssrInterpolate(new Date())
} \`)
}
import { useSSRContext as __vite_useSSRContext } from ' '
const _sfc_setup = _sfc_main.setup
_sfc_main.setup = (props, ctx) => {
const ssrContext = __vite_useSSRContext()
;(ssrContext.modules || (ssrContext.modules = new Set())).add(\\" \\")
return _sfc_setup ? _sfc_setup(props, ctx) : undefined
}
import _export_sfc from ' '
export default _export_sfc(_sfc_main, [[' ',_sfc_ssrRender],[' ',\\" \\"]])"
`)
})

0 comments on commit 2a35abf

Please sign in to comment.